//
//  OYProgressHUD.m
//  qpyd
//
//  Created by 掌游网络 on 2017/7/21.
//  Copyright © 2017年 掌游网络. All rights reserved.
//

#import "OYProgressHUD.h"
#import "MBProgressHUD.h"

@interface OYProgressHUD ()

@property (nonatomic,strong) MBProgressHUD  *hud;

@end

@implementation OYProgressHUD

+ (void)initialize {
    [OYProgressHUD shareInstance];
}

static OYProgressHUD *_instance = nil;
+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[OYProgressHUD alloc] init];
    });
    return _instance;
}

//显示
+ (void)show:(NSString *)msg inView:(UIView *)view mode:(OYProgressHUDMode)myMode offsetY:(CGFloat)offsetY {
    //如果已有弹框，先消失
    if (_instance.hud) {
        [_instance.hud hideAnimated:YES];
        _instance.hud = nil;
    }
    
    //4\4s屏幕避免键盘存在时遮挡
    if ([UIScreen mainScreen].bounds.size.height == 480) {
        [view endEditing:YES];
    }
    
    _instance.hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
//    _instance.hud.dimBackground = YES;    //是否显示透明背景
    _instance.hud.bezelView.color = [UIColor colorWithWhite:0 alpha:0.8];
    _instance.hud.bezelView.layer.cornerRadius = 2;
    switch (myMode) {
        case OYProgressHUDModeActivityLoading:
            [_instance.hud setMargin:15];
            break;
            
        default:
            [_instance.hud setMargin:10];
            break;
    }
    _instance.hud.offset = CGPointMake(0, offsetY);
    [_instance.hud setRemoveFromSuperViewOnHide:YES];
    _instance.hud.detailsLabel.text = msg ? msg : @"";
    _instance.hud.contentColor = [UIColor whiteColor];
    _instance.hud.detailsLabel.font = [UIFont systemFontOfSize:14];
    switch (myMode) {
        case OYProgressHUDModeOnlyText:
            _instance.hud.mode = MBProgressHUDModeText;
            break;
            
        case OYProgressHUDModeActivityLoading:
            _instance.hud.mode = MBProgressHUDModeIndeterminate;
            break;
            
        default:
            break;
    }
}

//隐藏
+ (void)hide {
    if (_instance.hud) {
        [_instance.hud hideAnimated:YES];
    }
}

//只显示文字
+ (void)showOnlyText:(NSString *)msg inView:(UIView *)view offsetY:(CGFloat)offsetY {
    [self show:msg inView:view mode:OYProgressHUDModeOnlyText offsetY:offsetY];
}

//显示提示（1秒后消失）
+ (void)showMessage:(NSString *)msg inView:(UIView *)view offsetY:(CGFloat)offsetY {
    [self show:msg inView:view mode:OYProgressHUDModeOnlyText offsetY:offsetY];
    [_instance.hud hideAnimated:YES afterDelay:1.0];
}

//显示提示（N秒后消失）
+ (void)showMessage:(NSString *)msg inView:(UIView *)view afterDelayTime:(NSInteger)delay offsetY:(CGFloat)offsetY {
    [self show:msg inView:view mode:OYProgressHUDModeOnlyText offsetY:offsetY];
    [_instance.hud hideAnimated:YES afterDelay:delay];
}

//在最上层显示
+ (void)showMsgWithoutView:(NSString *)msg offsetY:(CGFloat)offsetY {
    
    UIWindow *view = [[UIApplication sharedApplication] keyWindow];
    [self show:msg inView:view mode:OYProgressHUDModeOnlyText offsetY:offsetY];
    [_instance.hud hideAnimated:YES afterDelay:1.0];
}

@end
